package com.utp.freemarker.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.utp.freemarker.service.FreemarkerService;

import freemarker.template.TemplateException;

/**
 * @author Arnold Yand
 * @since Mar 21, 201711:32:36 AM
 * @summary com.utp.freemarker.controller.PersonController.java
 */
@Controller("personController")
@RequestMapping("/person")
public class PersonController {

    @Resource(name = "freemarkerService")
    private FreemarkerService freemarkerService;

    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public void personInfo(HttpServletResponse response, @PathVariable("id") String id) throws IOException, TemplateException {

        Map<String, Object> root = new HashMap<String, Object>();
        root.put("user", id);
        String view = freemarkerService.buildView(root, "index");

        response.setContentType("text/html");
        ServletOutputStream out = response.getOutputStream();
        out.write(view.getBytes());
        out.flush();
        out.close();
    }
}
